package com.rdc.qingkou.view.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.rdc.qingkou.Application.MyApplication;
import com.rdc.qingkou.R;
import com.rdc.qingkou.adapter.SubscribeRecyclerViewAdapter;
import com.rdc.qingkou.config.URL;
import com.rdc.qingkou.contract.SubscribeContract;
import com.rdc.qingkou.entity.SubscribeBean;
import com.rdc.qingkou.entity.SubscribeListResult;
import com.rdc.qingkou.entity.SubscribeUserResult;
import com.rdc.qingkou.presenter.SubscribePresenter;
import com.rdc.qingkou.ui.SubscribeActivity;

import java.util.ArrayList;
import java.util.List;


public class MeSubscribeFragment extends SubscribeFragmentBase implements SubscribeContract.ISubscribeView{

    //MyApplication.getUser().getUserId()

    public List<SubscribeBean> mSubscribeList;
    private SubscribeRecyclerViewAdapter mSubscribeRecyclerViewAdapter;
    private SubscribeActivity mActivity;
    public static final String ARG_PAGE = "ARG_PAGE";
    private Integer hasMessage;   //

    private SubscribePresenter mSubscribePresenter;

    private RecyclerView recyclerView;

    public static MeSubscribeFragment newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        MeSubscribeFragment pageFragment = new MeSubscribeFragment();
        pageFragment.setArguments(args);
        return pageFragment;
    }



    @Override
    public void onAttach(Context context) {


        super.onAttach(context);
        mActivity = (SubscribeActivity) getActivity();


        mSubscribePresenter = new SubscribePresenter(this);
        mSubscribePresenter.getSubscribeList(MyApplication.getUser().getUserId());
        mSubscribePresenter.getFansList(1);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mSubscribeList=new ArrayList<>();
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        subscribe_total_textView.setText("共订阅了"+mSubscribeList.size()+"名用户");
        mSubscribeRecyclerViewAdapter.notifyDataSetChanged();
    }

    @Override
    protected void refreshSubscribes() {

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mSubscribePresenter.getSubscribeList(MyApplication.getUser().getUserId());
                        swipeRefresh.setRefreshing(false);
                    }
                });
            }
        }).start();
    }

    @Override
    protected void setSubscribeRecyclerViewAdapter() {


//        RecyclerView recyclerView=getSubscribeRecyclerView();
        recyclerView = getSubscribeRecyclerView();

        mSubscribeRecyclerViewAdapter=new SubscribeRecyclerViewAdapter(mActivity,mSubscribeList);

        mSubscribeRecyclerViewAdapter.setOnSubscribeRecyclerViewListener(new SubscribeRecyclerViewAdapter.OnSubscribeRecyclerViewListener() {
            @Override
            public void onSubscribe(int position) {
//                Toast.makeText(mActivity,"subscribe :"+position,Toast.LENGTH_SHORT).show();

                if (mSubscribeList.get(position).getAddSubscribe().equals(R.drawable.button_subscribe_normal+ "")){
                    mSubscribeList.get(position).setAddSubscribe(R.drawable.button_subscribe_pressure+ "");
                    mSubscribeRecyclerViewAdapter.notifyDataSetChanged();
                    mSubscribePresenter.subscribeUser(MyApplication.getUser().getUserId(),mSubscribeList.get(position).getUserId());
                }else {
                    mSubscribeList.get(position).setAddSubscribe(R.drawable.button_subscribe_normal+ "");
                    mSubscribeRecyclerViewAdapter.notifyDataSetChanged();
                    mSubscribePresenter.subscribeUser(MyApplication.getUser().getUserId(),mSubscribeList.get(position).getUserId());
                }


            }

            @Override
            public void onItemClick(int position) {
//                Toast.makeText(mActivity,"you click:"+position,Toast.LENGTH_SHORT).show();
            }


        });
        recyclerView.setAdapter(mSubscribeRecyclerViewAdapter);

    }

    @Override
    public void showBottom(int lastIndex) {

    }

    @Override
    public Context getCurContext() {
        return getContext();
        //return null;
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void showData(SubscribeListResult subscribeResult) {

        if (subscribeResult.getUserSubscribeList().size() != 0){
            hasMessage = 0;

            Log.d("MeSubscribeFragment" ,"showData");

            List<SubscribeBean> subscribeBeanList = new ArrayList<>();
            for (int i = 0; i < subscribeResult.getUserSubscribeList().size(); i++) {
                SubscribeBean subscribeBean = new SubscribeBean();

                subscribeBean.setUserId(subscribeResult.getUserSubscribeList().get(i).getUserId());
                subscribeBean.setContent(subscribeResult.getUserSubscribeList().get(i).getSignature());
                subscribeBean.setName(subscribeResult.getUserSubscribeList().get(i).getNickName());

                subscribeBean.setHeadSculpturePath(URL.HEAD_YUN2+subscribeResult.getUserSubscribeList().get(i).getImageUrl()+"");


                subscribeBean.setAddSubscribe(R.drawable.button_subscribe_pressure+ "");
                subscribeBeanList.add(subscribeBean);
            }
            mSubscribeList.clear();
            mSubscribeList.addAll(subscribeBeanList);

            subscribe_total_textView.setText("共订阅了"+mSubscribeList.size()+"名用户");

            mSubscribeRecyclerViewAdapter.notifyDataSetChanged();
        }else {
            hasMessage = 1;
//            SubscribeBean subscribeBean = new SubscribeBean();
//            subscribeBean.setContent("你还没有订阅喔");
//            subscribeBean.setAddSubscribe(R.drawable.button_subscribe_pressure+ "");   //???????
//            mSubscribeList.clear();
//            mSubscribeList.add(subscribeBean);
//            subscribe_total_textView.setText("共订阅了"+mSubscribeList.size()+"名用户");
//            mSubscribeRecyclerViewAdapter.notifyDataSetChanged();

            recyclerView.setBackground(getCurContext().getResources().getDrawable(R.drawable.subscribe_none_subscribe));
        }
    }

    @Override
    public void showInfo(String info) {}

    @Override
    public void showSubscribeList(SubscribeListResult list) {
        //Toast.makeText(getActivity(),"showSubscribeList result : " + list.getResult(),Toast.LENGTH_SHORT).show();
        Log.d("MeSubscribeFragment","showSubscribeList result : " + list.getResult());
        showData(list);
    }

    @Override
    public void subscribeUser(SubscribeUserResult reason) {
        //Toast.makeText(getActivity(),"subscribeUser : " +reason.getResult(),Toast.LENGTH_SHORT).show();
        Log.d("MeSubscribeFragment","reason.getResult() : "+ reason.getResult());
        Log.d("MeSubscribeFragment","reason.getReason() : "+ reason.getReason());
    }

    @Override
    public void showFansList(SubscribeListResult list) {
        //Toast.makeText(getActivity(),"showSubscribeFansList list  : " +list.getResult(),Toast.LENGTH_SHORT).show();
    }
}
